import axios, { AxiosError } from "axios";
import { Store } from "../main";
import ServerInfo from "../static/json/baseServer.json";
export interface ErrorRequest extends AxiosError {
  ErrorStatus: boolean;
}
export default class AbstractService {
  private RequestInstance = axios.create({
    baseURL: ServerInfo.baseUrl,
    timeout: ServerInfo.timeOut,
  });
  protected async GetData(url: string, data: any = null) {
    try {
      return await this.RequestInstance({
        method: "get",
        url: url,
        params: { ...data },
        headers: Store.Common.getRequestHeader(),
      }).then(res => res.data);
    } catch (err: any) {
      err["ErrorStatus"] = true;
      return Promise.resolve(err);
    }
  }
  protected async PostData(url: string, data: any = null) {
    try {
      return await this.RequestInstance({
        method: "post",
        url: url,
        data: { ...data },
        headers: Store.Common.getRequestHeader(),
      }).then(res => res.data);
    } catch (err: any) {
      err["ErrorStatus"] = true;
      return Promise.resolve(err);
    }
  }
  protected async PutData(url: string, data: any = null) {
    try {
      return await this.RequestInstance({
        method: "put",
        url: url,
        data: { ...data },
        headers: Store.Common.getRequestHeader(),
      }).then(res => res.data);
    } catch (err: any) {
      err["ErrorStatus"] = true;
      return Promise.resolve(err);
    }
  }
  protected async DeleteData(url: string, data: any = null) {
    try {
      return await this.RequestInstance({
        method: "delete",
        url: url,
        data: { ...data },
        headers: Store.Common.getRequestHeader(),
      }).then(res => res.data);
    } catch (err: any) {
      err["ErrorStatus"] = true;
      return Promise.resolve(err);
    }
  }
}
